package parse;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import partitions.Partition;

public class FileInputTemplate {

	public static void main(String[] args) {
		
		if(args.length == 0){
			System.out.println("must supply number of agents as argument");
		}
		int numAgents = -1;
		try{
			numAgents = Integer.parseInt(args[0]);
			System.out.println("creating template for " + numAgents + " agents");
		} catch (NumberFormatException ex) {
			System.out.println("must supply number of agents as argument");
			System.err.println("Not a valid number: " + args[0]);
			System.exit(1);
		}			
			
		FileInputTemplate.generate(numAgents);
	}
	
	public static void generate(int numAGents){
		
		Partition partitionDomain = new Partition(numAGents);
		List<int[]> partitions = partitionDomain.getPartitions();
		String extString = createZeroExternalityArrayString(numAGents);
	    try {
	        BufferedWriter out = new BufferedWriter(new FileWriter("template_" + numAGents + "_agents.txt"));
	        out.write(numAGents + "\n");
	        for(int[] partition : partitions){
				String partitionString = Partition.partitionToExpandedString(partition);
				out.write(partitionString + " " + extString + "\n");
			}
	        
	        out.close();
	    } catch (IOException e) {
	    	System.out.println("unable to create or open file for writing");
	    }
		
	}
	
	public static String createZeroExternalityArrayString(int numAgents){
		StringBuffer arrayString = new StringBuffer("{");
		for(int i = 0; i < numAgents - 1; i++)
			arrayString.append("0" + ", ");
		if(numAgents > 0)
			arrayString.append("0");
		arrayString.append("}");
		return arrayString.toString();
	}
	
}
